package com.yantu.controller.student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yantu.bean.*;
import com.yantu.mapper.*;
import com.yantu.util.Result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@ApiModel("答卷查询接口信息")
@RequestMapping("/student")
public class StudentAnswerPaperSelectController {


    @Autowired
    ExamPaperMapper examPaperMapper;
    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;
    @Autowired
    TextContentMapper textContentMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    ExamPaperQuestionCustomerAnswerMapper examPaperQuestionCustomerAnswerMapper;


    //答卷查询
    @GetMapping("/AnswerPaperSelect/{id}")
    @ApiOperation("答卷查询")
    public Result AnswerPaperSelect(@PathVariable("id") Integer id) {

        ExamPaperAnswer examPaperAnswer = examPaperAnswerMapper.selectById(id);
        HashMap<String, Object> map = new HashMap<>();
//        List<Object> list = new ArrayList();

        //-------------------此处为answer--------------
        //-------------------此处为answer--------------
        //答卷信息"answer"
        HashMap<String, Object> map5 = new HashMap<>();
        map5.put("id",examPaperAnswer.getId());
        map5.put("doTime",examPaperAnswer.getDoTime());
        map5.put("score",examPaperAnswer.getUserScore());
        //list4用来存 "answerItems"
        List<Object> list4 = new ArrayList();
        //-------------------此处为answer--------------
        //-------------------此处为answer--------------

        //试卷信息"paper"
        HashMap<String, Object> map1 = new HashMap<>();
        ExamPaper examPaper = examPaperMapper.selectById(examPaperAnswer.getExamPaperId());
        map1.put("id",examPaper.getId());
        map1.put("level",examPaper.getGradeLevel());
        map1.put("subjectId",examPaper.getSubjectId());
        map1.put( "paperType",examPaper.getPaperType());
        map1.put("name",examPaper.getName());
        map1.put("suggestTime", examPaper.getSuggestTime());
        List<Object> list1 = new ArrayList();
        list1.add(examPaper.getLimitStartTime());
        list1.add(examPaper.getLimitEndTime());
        map1.put("limitDateTime",list1);

//     "titleItems"


        TextContent textContent = textContentMapper.selectById(examPaper.getFrameTextContentId());
        JSONArray contentArray = JSONArray.fromObject(textContent.getContent());

        //将"questionItems"和"name"放入"titleItems"
        List<Object> list5 = new ArrayList();

        //大题
        for (int k = 0; k < contentArray.size(); k++) {
            //      获取content表中的content内容
            HashMap<String, Object> map2 = new HashMap<>();
            JSONObject content=contentArray.getJSONObject(k);
            map2.put("name",content.getString("name"));
            //获取题目列表
            // "questionItems"
            JSONArray questionItemArray=JSONArray.fromObject(content.getString("questionItems"));

            List<Object> list2 = new ArrayList();
            for(int i = 0; i <questionItemArray.size() ; i++) {
                JSONObject question = questionItemArray.getJSONObject(i);
                HashMap<String, Object> map3 = new HashMap<>();
                map3.put("id", question.getString("id"));
                Question questionitem = questionMapper.selectById(question.getString("id"));
                map3.put("questionType", questionitem.getQuestionType());
                map3.put("gradeLevel", questionitem.getGradeLevel());
                map3.put("subjectId", questionitem.getSubjectId());
                //小题里的题目内容
                TextContent textContent1 = textContentMapper.selectById(questionitem.getInfoTextContentId());
                JSONObject content1 = JSONObject.fromObject(textContent1.getContent());
                map3.put("title",content1.getString("titleContent"));

                //小题里的选项
                JSONArray questionItemArray1=JSONArray.fromObject(content1.getString("questionItemObjects"));
                if(questionitem.getQuestionType()==1||questionitem.getQuestionType()==2||questionitem.getQuestionType()==3){

                    List<Object> list3 = new ArrayList();
                    for (int j = 0; j <questionItemArray1.size() ; j++) {
                        JSONObject questionObject = questionItemArray1.getJSONObject(j);
                        HashMap<String, Object> map4 = new HashMap<>();
                        map4.put("prefix",questionObject.getString("prefix"));
                        map4.put("content",questionObject.getString("content"));
                        map4.put("score",questionObject.getString("score"));
                        list3.add(map4);
                    }
                    map3.put("items",list3);

                }
                else {
                    map3.put("items",null);

                }



                map3.put("analyze",content1.getString("analyze"));
                map3.put("correct",content1.getString("correct"));
//          找不到correctArry
                map3.put("correctArray",null);
                map3.put("score",questionitem.getScore());
                map3.put("difficult",questionitem.getDifficult());
                map3.put("itemOrder",question.getString("itemOrder"));
                list2.add(map3);
                map2.put("questionItems",list2);

                //-------------------此处为answer--------------------
                //-------------------此处为answer--------------
                //"answer"里的"answerItems"
                HashMap<String, Object> map6 = new HashMap<>();
                QueryWrapper<ExamPaperQuestionCustomerAnswer> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("exam_paper_answer_id",examPaperAnswer.getId());
                queryWrapper.eq("question_id",questionitem.getId());
                ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = examPaperQuestionCustomerAnswerMapper.selectOne(queryWrapper);
                map6.put("id",examPaperQuestionCustomerAnswer.getId());
                map6.put("questionId",questionitem.getId());
                map6.put("doRight",examPaperQuestionCustomerAnswer.getDoRight());

                if(examPaperQuestionCustomerAnswer.getTextContentId()!=null){
                    TextContent textContent2 = textContentMapper.selectById(examPaperQuestionCustomerAnswer.getTextContentId());
                    if (examPaperQuestionCustomerAnswer.getQuestionType()==4){
                        map6.put("content",null);
                        map6.put("contentArray",textContent2.getContent());

                    }
                    else if(examPaperQuestionCustomerAnswer.getQuestionType()==5){
                        map6.put("content",textContent2.getContent());
                        map6.put("contentArray",null);
                    }
                }
                else {
                    map6.put("content", null);
                    map6.put("contentArray", null);
                }
                map6.put("itemOrder",question.getString("itemOrder"));
                map6.put("score",examPaperQuestionCustomerAnswer.getCustomerScore());
                map6.put("questionScore",examPaperQuestionCustomerAnswer.getQuestionScore());
                list4.add(map6);
                map5.put("answerItems",list4);
                //-------------------此处为answer----------------------
                //-------------------此处为answer--------------

            }

            list5.add(map2);



        }


        map1.put("titleItems",list5);
        map1.put("score",examPaper.getScore());

        map.put("paper",map1);
        map.put("answer",map5);




        return Result.succ(200, "查询成功",map);

    }
}
